<?php

//供应商

class Supplier extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('supplier_model');
	}

	public function index()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;
		
		$per_page = 20;

		$count = $this->supplier_model->get_count();
		$supplier_list = $this->supplier_model->get_page($p, $per_page);
		
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('supplier/index'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();
		
		$this->template->assign('list', $supplier_list);
		$this->template->assign('page', $this->page);
		$this->template->display('supplier/list');
	}

	//搜索
	public function search()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;

		$keywords = trim(get('keywords'));
		
		$per_page = 20;

		$result = $this->db->select('count(*) as count')->from('supplier')->like('name', $keywords)->or_like('contacter', $keywords)->get()->row_array();
		$count = $result['count'];

		$supplier_list = $this->db->select('id, name, contacter, phone, tel')->from('supplier')->like('name', $keywords)->or_like('contacter', $keywords)->limit($per_page, ($p-1)*$per_page)->get()->result_array();
		
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('supplier/search'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();
		
		$this->template->assign('list', $supplier_list);
		$this->template->assign('page', $this->page);
		$this->template->display('supplier/list');
	}

	//添加供应商页面
	protected function _insert_get()
	{
		$this->template->display('supplier/insert');
	}
	
	//提交添加供应商
	protected function _insert_post()
	{
		$this->load->helper('validation');
		
		$name = trim(post('name')); //供应商名称
		$contacter = trim(post('contacter')); //联系人
		$phone = trim(post('phone')); //手机
		$tel = trim(post('tel')); //电话
		$email = trim(post('email')); //邮箱
		$zip = trim(post('zip')); //邮编
		$address = trim(post('address')); //地址
		$remark = trim(post('remark')); //备注信息
		
		if ($name == '')
		{
			msg('名称必须填写！', -1);
		}
		elseif ( ! $contacter)
		{
			msg('联系人必须填写！', -1);
		}
		elseif ($phone && ! is_phone($phone))
		{
			msg('手机格式错误！', -1);
		}
		elseif ($tel && ! is_tel($tel))
		{
			msg('电话格式错误！', -1);
		}
		elseif ($email && ! is_email($email))
		{
			msg('邮箱格式错误！', -1);
		}
		elseif ($zip && ! is_zip($zip))
		{
			msg('邮编格式错误！', -1);
		}
		elseif ( ! $phone && ! $tel)
		{
			msg('手机或电话必须填其一个！', -1);
		}
		
		if ($this->supplier_model->exists(array('name' => $name)))
		{
			msg('供应商名称已经存在！', -1);
		}
		
		$data = array(
			'name' => $name,
			'contacter' => $contacter,
			'phone' => $phone,
			'tel' => $tel,
			'email' => $email,
			'zip' => $zip,
			'address' => $address,
			'remark' => $remark
		);

		if ( ! ($id = $this->supplier_model->insert($data)))
		{
			log_message('error', '添加供应商失败！原因：写入supplier表失败！');
			msg('添加失败！');
		}
		
		$msg = '添加成功！<br /><br /><a href="'.admin_url('supplier/update', array('id' => $id)).'" class="button">返回修改</a>&nbsp;&nbsp;&nbsp;<a href="'.admin_url('supplier/insert').'" class="button">继续添加</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//修改页面
	protected function _update_get()
	{
		$id = intval(get('id'));
		if ($id < 1)
		{
			msg('非法访问！');
		}
		
		$supplier_data = $this->supplier_model->get($id);
		if ( ! $supplier_data)
		{
			msg('供应商不存在！', admin_url('supplier/index'));
		}
		
		$this->template->assign('data', $supplier_data);
		$this->template->display('supplier/update');
	}
	
	//提交修改信息
	protected function _update_post()
	{
		$this->load->helper('validation');
		
		$id = intval(post('id')); //供应商id
		$name = trim(post('name')); //供应商名称
		$contacter = trim(post('contacter')); //联系人
		$phone = trim(post('phone')); //手机
		$tel = trim(post('tel')); //电话
		$email = trim(post('email')); //邮箱
		$zip = trim(post('zip')); //邮编
		$address = trim(post('address')); //地址
		$remark = trim(post('remark')); //备注信息
		
		if ($id < 1)
		{
			msg('非法访问！');
		}
		elseif ($name == '')
		{
			msg('名称必须填写！', -1);
		}
		elseif ( ! $contacter)
		{
			msg('联系人必须填写！', -1);
		}
		elseif ($phone && ! is_phone($phone))
		{
			msg('手机格式错误！', -1);
		}
		elseif ($tel && ! is_tel($tel))
		{
			msg('电话格式错误！', -1);
		}
		elseif ($email && ! is_email($email))
		{
			msg('邮箱格式错误！', -1);
		}
		elseif ($zip && ! is_zip($zip))
		{
			msg('邮编格式错误！', -1);
		}
		elseif ( ! $phone && ! $tel)
		{
			msg('手机或电话必须填其一个！', -1);
		}
		
		$supplier_data = $this->supplier_model->get($id);
		if ( ! $supplier_data)
		{
			msg('供应商不存在！', admin_url('supplier/index'));
		}
		
		if ($supplier_data['name'] != $name)
		{
			if ($this->supplier_model->exists(array('name' => $name)))
			{
				msg('供应商名称已经存在！', -1);
			}
		}
		
		$data = array(
			'name' => $name,
			'contacter' => $contacter,
			'phone' => $phone,
			'tel' => $tel,
			'email' => $email,
			'zip' => $zip,
			'address' => $address,
			'remark' => $remark
		);
		
		if (FALSE === $this->supplier_model->update($data, $id))
		{
			log_message('error', '修改供应商失败！原因：更新supplier表失败！');
			msg('修改失败！');
		}
		
		$msg = '修改成功！<br /><br /><a href="'.admin_url('supplier/update', array('id' => $id)).'" class="button">继续修改</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//删除供应商
	public function _delete_ajax()
	{
		$id = intval(get('id'));
		
		if ($id < 1)
		{
			ajax(array('error' => 1, 'msg' => '非法请求！'));
		}
		
		$result = $this->supplier_model->delete($id);
		
		if ($result !== FALSE)
		{
			ajax(array('error' => 0, 'msg' => '删除成功！'));
		}
		else
		{
			log_message('error', '删除供应商失败！原因：更新supplier表失败！');
			ajax(array('error' => 2, 'msg' => '删除失败！'));
		}
	}

	//导出excel
	public function export()
	{
		$type = get('type');
		if ($type == 'excel')
		{
			$this->_export_to_excel();
		}
		elseif ($type == 'txt')
		{
			$this->_export_to_txt();
		}
	}

	//导出excel
	public function _export_to_excel()
	{
		$this->load->library('PHPExcel', NULL, 'phpexcel');
		$this->phpexcel->setActiveSheetIndex(0)
			->setCellValue('A1', 'ID')
			->setCellValue('B1', '名称')
			->setCellValue('C1', '联系人')
			->setCellValue('D1', '手机')
			->setCellValue('E1', '电话')
			->setCellValue('F1', '邮箱')
			->setCellValue('G1', '地址')
			->setCellValue('H1', '邮编')
			->setCellValue('I1', '备注');

		//设置列宽度
		$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
		$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
		$this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
		$this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
		$this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
		$this->phpexcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
		$this->phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
		$this->phpexcel->getActiveSheet()->getColumnDimension('I')->setWidth(50);

		//设置字体粗体
		$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('G1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('H1')->getFont()->setBold(TRUE);
		$this->phpexcel->getActiveSheet()->getStyle('I1')->getFont()->setBold(TRUE);

		$list = $this->supplier_model->get_all();
		$i = 2;

		foreach ($list as $row)
		{
			$this->phpexcel->setActiveSheetIndex(0)
				->setCellValue('A'.$i, $row['id'])
				->setCellValue('B'.$i, $row['name'])
				->setCellValue('C'.$i, $row['contacter'])
				->setCellValue('D'.$i, $row['phone'])
				->setCellValue('E'.$i, $row['tel'])
				->setCellValue('F'.$i, $row['email'])
				->setCellValue('G'.$i, $row['address'])
				->setCellValue('H'.$i, $row['zip'])
				->setCellValue('I'.$i, $row['remark']);
		}

		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="供应商-'.date('Y-m-d').'.xls"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
		$objWriter->save('php://output');
	}

	//导出txt
	public function _export_to_txt()
	{
		$list = $this->supplier_model->get_all();
		$txt = "ID\t名称\t联系人\t手机\t电话\t邮箱\t地址\t邮编\t备注\n";
		if ($list)
		{
			foreach ($list as $row)
			{
				$txt .= $row['id']."\t".$row['name']."\t".$row['contacter']."\t".$row['phone']."\t".$row['tel']."\t".$row['email']."\t".$row['address']."\t".$row['zip']."\t".$row['remark']."\n";
			}
		}

		$this->load->helper('download');
		force_download('供应商-'.date('Y-m-d').'.txt', $txt);
	}
	
}